package com.ray.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ray.activiti.dto.ProcessTaskDto;
import com.ray.activiti.service.ActModelerService;
import com.ray.activiti.service.ExtendActModelerService;
import com.ray.activiti.service.ExtendActNodesetService;
import com.ray.activiti.service.ExtendActNodeuserService;
import com.ray.activiti.table.entity.ExtendActModel;
import com.ray.activiti.table.entity.ExtendActNodeset;
import com.ray.activiti.vo.ActNode;
import com.ray.api.service.ActivitiApiService;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.web.base.BaseService;
/**
 * 流程中心
 * @author ray
 *
 */
@Service
public class ActivitiApiServiceImpl extends BaseService implements ActivitiApiService{
    
	@Autowired
	ActModelerService actModelerService;
	@Autowired
	ExtendActModelerService extendActModelService;
	@Autowired
	ExtendActNodeuserService nodeuserService;
	@Autowired
	ExtendActNodesetService nodesetService;
	
	@Override
	public void startFlow(ProcessTaskDto taskDto,ActNode startNode,LoginUser user) throws Exception {
		actModelerService.startFlowAndExeNextTask(taskDto,startNode, user);
	}

	@Override
	public void startFlow(String busId, String actKey,String userids,String deployId,String defId, LoginUser user) throws Exception {

        ActNode actNode = actModelerService.getStartFlowInfo(deployId);
        if(actNode == null){
        	throw new BussinessException(-1, "无法找到下一个审批节点");
        }
        
        ProcessTaskDto taskDto = new ProcessTaskDto();
        taskDto.setActKey(actKey);
        taskDto.setBusId(busId);
        taskDto.setDefId(defId);
        //流程启动默认第一个节点是自己 这里配置自己后面一个节点
        taskDto.setNextUserIds(userids);
        //第一个必须是审批节点
        taskDto.setNodeType(actNode.getNodeType());
        taskDto.setStartUserName(user.getUserName());
        actModelerService.startFlowAndExeNextTask(taskDto,actNode, user);
		
	}
	
	@Override
	public void HandleTask(ProcessTaskDto taskDto,LoginUser user)throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		actModelerService.doActTask(taskDto, params, user);
	}

	@Override
	public void backStartUser(ProcessTaskDto processTaskDto, LoginUser user) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		actModelerService.backStartUser(processTaskDto, params, user);
	}
	
	@Override
	public void stopFlow(ProcessTaskDto processTaskDto, LoginUser user) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		actModelerService.stopFlow(processTaskDto, params, user);
	}

	@Override
	public void turnToDo(ProcessTaskDto processTaskDto, String toUserId, LoginUser user) throws Exception {
		actModelerService.turnToDo(processTaskDto, toUserId, user);
	}
	
	@Override
	public void backPreviousNode(ProcessTaskDto processTaskDto,LoginUser user) throws Exception {
		actModelerService.backPreviousNode(processTaskDto,user);
	}

	@Override
	public List<Map<String, Object>> queryFlowsByActKey(String actKey,LoginUser user) {
		List<Map<String, Object>> defs = actModelerService.queryFlowsByActKey(actKey,user);
		return defs;
	}

	@Override
	public ExtendActModel queryByDeployId(String deployid) {
		ExtendActModel extendActModel = extendActModelService.queryByDeployId(deployid);
		return extendActModel;
	}

	@Override
	public List<String> queryIds(String type, String nodeId) {
		return nodeuserService.queryIds(type, nodeId);
	}

	@Override
	public ExtendActNodeset queryStartNodeByModelId(String modelId) {
		ExtendActNodeset node = nodesetService.queryStartNodeByModelId(modelId);
		return node;
	}

	
	
	
	
	
}
